<template>
	<div class="system-dicManager-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="700px">

			<el-card shadow="hover">
				<el-form ref="editDialogFormRef" :model="state.ruleForm" :rules="rules" size="default" label-width="120px">
					<el-row :gutter="35">
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="币种名称" prop="coinName">
								<el-input v-model="state.ruleForm.coinName" placeholder="币种名称" clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="币种图片" prop="coinImage">
								<uploadItem @changeImg="changeImgImage" :imgPath="state.ruleForm.coinImage"></uploadItem>
							</el-form-item>
						</el-col>


						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="币种简称" prop="coinSymbol">
								<el-input v-model="state.ruleForm.coinSymbol" placeholder="币种简称" clearable></el-input>
							</el-form-item>
						</el-col>

						<!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="支付合约" prop="payContract">
								<el-input v-model="state.ruleForm.payContract" placeholder="支付合约" clearable></el-input>
							</el-form-item>
						</el-col>


						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="代币合约" prop="tokenContract">
								<el-input v-model="state.ruleForm.tokenContract" placeholder="代币合约" clearable></el-input>
							</el-form-item>
						</el-col> -->

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="单日提现总量" prop="withdrawPerDayLimit">
								<el-input v-model="state.ruleForm.withdrawPerDayLimit" placeholder="单日提现总量"
									clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="提现审核阈值" prop="withdrawAuthLimit">
								<el-input v-model="state.ruleForm.withdrawAuthLimit" placeholder="提现审核阈值"
									clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="最小提现" prop="withdrawMinAmount">
								<el-input v-model="state.ruleForm.withdrawMinAmount" placeholder="最小提现"
									clearable></el-input>
							</el-form-item>
						</el-col>




						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="提现费率" prop="withdrawChargeType">
								<el-select v-model="state.ruleForm.withdrawChargeType" clearable="true">
									<dicItemsSelectOptions dicCode="accountWithdrawChargeType" data-type="Int" />
								</el-select>
							</el-form-item>
						</el-col>




						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="提现费率" prop="withdrawCharge">
								<el-input v-model="state.ruleForm.withdrawCharge" placeholder="提现费率" clearable></el-input>
							</el-form-item>
						</el-col>


						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="状态" prop="status">
								<el-select clearable v-model="state.ruleForm.status">
									<el-option value="10" label='可用'></el-option>
									<el-option value="20" label='仅充值'></el-option>
									<el-option value="30" label='仅提现'></el-option>
									<el-option value="99" label='禁用'></el-option>
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="转账状态" prop="transferStatus">
								<el-switch v-model="state.ruleForm.transferStatus"></el-switch>
							</el-form-item>
						</el-col>



						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="谷歌验证码" prop="code">
								<el-input v-model="state.ruleForm.code" placeholder="谷歌验证码" clearable></el-input>
							</el-form-item>
						</el-col>




					</el-row>
				</el-form>


			</el-card>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>

	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref, watch, defineAsyncComponent } from 'vue';


const uploadItem = defineAsyncComponent(() => import('./uploadItem.vue'));

import { useFundApi } from '/@/api/fund/index'

const dicItemsSelectOptions = defineAsyncComponent(() => import('/@/components/dic/dicItemsSelectOptions.vue'));

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const editDialogFormRef = ref();

const rules = {
	
	code: [
		{ required: true, message: "请输入谷歌验证码", trigger: "blur" }
	],
	coinName: [
		{ required: true, message: "请输入币种名称", trigger: "blur" }
	],
	coinSymbol: [
		{ required: true, message: "请输入币种符号", trigger: "blur" }
	],
	status: [
		{ required: true, message: "请选择状态", trigger: "blur" }
	]
}


const state = reactive({
	row: null as any,
	systemTypeSelect: [] as any,
	ruleForm: {
		code: "",
		coinName: "",
		coinSymbol: "",
		// payContract: "",
		// tokenContract: "",
		withdrawPerDayLimit: "",
		withdrawAuthLimit: "",
		withdrawMinAmount: "",
		withdrawCharge: "",
		status: "",
		withdrawChargeType: 10,
		transferStatus: false,
		coinImage: ""
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});



// 打开弹窗
const openDialog = async (type: string, row?: any) => {

	// systemAddressType();



	if (type === 'edit') {
		state.row = row;
		state.ruleForm = {
			code: "",
			coinName: row.coinName,
			coinSymbol: row.coinSymbol,
			// payContract: row.payContract,
			// tokenContract: row.tokenContract,
			withdrawPerDayLimit: row.withdrawPerDayLimit,
			withdrawAuthLimit: row.withdrawAuthLimit,
			withdrawMinAmount: row.withdrawMinAmount,
			withdrawCharge: row.withdrawCharge,
			status: row.status.toString(),
			transferStatus: row.transferStatus == 10 ? true : false,
			withdrawChargeType: row.withdrawChargeType,
			coinImage: row.coinImage
		}

		state.dialog.title = '修改币种信息';
		state.dialog.submitTxt = '修 改';
	} else {

		state.ruleForm = {
			code: "",
			coinName: "",
			coinSymbol: "",
			// payContract: "",
			// tokenContract: "",
			withdrawPerDayLimit: "",
			withdrawAuthLimit: "",
			withdrawMinAmount: "",
			withdrawCharge: "",
			status: "",
			withdrawChargeType: 10,
			transferStatus: false,
			coinImage: ""
		}

		state.dialog.title = '新增币种信息';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};


const systemAddressType = async () => {
	let listTemp = await useFundApi().systemAddressType();
	state.systemTypeSelect = [];
	listTemp.forEach((e: any) => {
		state.systemTypeSelect.push({
			i18nValue: e.i18nValue,
			name: e.name,
			value: e.value
		})
	});
}


const changeImgImage = (e) => {
	state.ruleForm.coinImage = e.coverImage
}


// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};


// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {


	editDialogFormRef.value.validate(async (valid: any) => {
		if (valid) {
			//检查参数列表
			try {
				if (state.dialog.type == 'edit') {
					let obj = Object.assign({}, state.row, state.ruleForm) as any;
					obj["transferStatus"] = obj.transferStatus ? 10 : 20;
					await useFundApi().updateCoin(obj, state.ruleForm.code);
				}
				ElMessage.success("操作成功");
				closeDialog(); // 关闭弹窗
				emit('refresh');
			} catch (error: any) {
				ElMessage.error("操作失败请稍后再试");
			}


		} else {
			ElMessage.warning("请检查数据的正确性！");
		}
	})




};




watch(
	() => state.dialog.isShowDialog,
	(newValue) => {
		if (newValue == true) {
		}
	}, { immediate: true }
);




// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style lang="scss">
.el-select {
	width: 100% !important;
}



.system-dicManager-dialog-container {
	.bar_tools {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 10px;
	}

	.el-table__expanded-cell {
		padding: 20px;
	}
}
</style>